<template>
  <div class="app-container">
    <el-form ref="filterForm" :inline="true" :model="listQuery" class="filter-container">
      <el-form-item prop="id">
        <el-input v-model="listQuery.id" size="small" placeholder="ID" clearable style="width: 100px;" @keyup.enter.native="handleFilter" />
      </el-form-item>
      <el-form-item prop="title">
        <el-input v-model="listQuery.title" size="small" placeholder="标题" clearable style="width: 200px;" @keyup.enter.native="handleFilter" />
      </el-form-item>

      <el-form-item prop="platformId">
        <el-select v-model="listQuery.platformId" size="small" placeholder="直播平台" filterable clearable style="width: 200px" @change="handleFilter">
          <el-option v-for="item in platformOptions" :key="item.id" :label="item.platformName" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item prop="isOnline">
        <el-select v-model="listQuery.isOnline" size="small" placeholder="是否上线" clearable style="width: 110px" @change="handleFilter">
          <el-option v-for="item in onlineOptions" :key="item.id" :label="item.name" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
        >筛选</el-button>
        <el-button
          size="small"
          @click="resetFilter"
        >重置</el-button>
        <el-button
          size="small"
          type="success"
          icon="el-icon-plus"
          @click="toLiveBroadcastNew"
        >添加直播</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      stripe
      fit
      highlight-current-row
    >
      <el-table-column label="ID" width="80" align="center">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="标题">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="直播平台" width="120" align="center">
        <template slot-scope="scope">
          {{ scope.row.platformName }}
        </template>
      </el-table-column>
      <el-table-column label="直播地址">
        <template slot-scope="scope">
          {{ scope.row.broadcastUrl }}
        </template>
      </el-table-column>
      <el-table-column label="关联会议">
        <template slot-scope="scope">
          <li
            v-for="item in scope.row.meetingList"
            :key="item.id"
          >
            <router-link
              :to="`/meetings/list/${item.id}?type=invite`"
            >{{ item.meetingtitle }}</router-link>
          </li>
        </template>
      </el-table-column>
      <el-table-column label="会议ID" width="120" align="center">
        <template slot-scope="scope">
          <el-tag
            v-for="item in scope.row.meetingList"
            :key="item.id"
            effect="plain"
            size="mini"
          >{{ item.id }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="频道ID" width="150" align="center">
        <template slot-scope="scope">
          {{ scope.row.channeId }}
        </template>
      </el-table-column>
      <el-table-column label="状态" width="100" align="center">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.status === 0"
            effect="plain"
            size="mini"
            type="warning"
          >未开始</el-tag>
          <el-tag
            v-else-if="scope.row.status === 1"
            effect="plain"
            size="mini"
            type="success"
          >进行中</el-tag>
          <el-tag
            v-else-if="scope.row.status === 2"
            effect="plain"
            size="mini"
            type="info"
          >已结束</el-tag>
          <el-tag
            v-else
            effect="plain"
            size="mini"
            type="info"
          >暂无</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="上线" width="60" align="center">
        <template slot-scope="scope">
          <i v-if="scope.row.isOnline === 1" class="el-icon-check text-success" />
          <i v-if="scope.row.isOnline === 0" class="el-icon-close text-warning" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-dropdown trigger="click">
            <el-button
              size="small"
              plain
            >操作 <i class="el-icon-arrow-down el-icon--right" /></el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item
                @click.native="toLiveBroadcastEdit(scope.row.id)"
              >编辑</el-dropdown-item>
              <!-- <el-dropdown-item
                divided
                @click.native="toLiveBroadcastReservation(scope.row.id)"
              >预约记录</el-dropdown-item> -->
              <el-dropdown-item
                v-if="scope.row.platformId === 1 || scope.row.platformId === 2"
                divided
                @click.native="toLiveBroadcastRecords(scope.row.id)"
              >直播记录</el-dropdown-item>
              <el-dropdown-item
                divided
                @click.native="removeLiveBroadcast(scope.row.id)"
              >
                <span class="text-danger">删除</span>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.size" @pagination="fetchData" />
  </div>
</template>

<script>
import {
  getLiveBroadcastsList,
  removeLiveBroadcast,
  getPlatformList
} from '@/api/liveBroadcasts'
import Pagination from '@/components/Pagination'
import {
  formatQuery
} from '@/utils'

export default {
  name: 'LiveBroadcastsList',
  components: {
    Pagination
  },
  data() {
    return {
      list: null,
      listLoading: true,
      total: 0,
      listQuery: {
        id: formatQuery(this.$route.query.id, false),
        title: formatQuery(this.$route.query.title, false),
        platformId: formatQuery(this.$route.query.platformId, true),
        isOnline: formatQuery(this.$route.query.isOnline, true),
        page: 1,
        size: 30
      },
      onlineOptions: [
        {
          name: '是',
          id: 1
        },
        {
          name: '否',
          id: 0
        }
      ],
      platformOptions: []
    }
  },
  // watch: {
  //   $route: {
  //     handler(route) {
  //       if (!this._.isNil(route.query.page) &&
  //         !this._.isEmpty(route.query.page)
  //       ) {
  //         this.listQuery.page = parseInt(route.query.page)
  //       } else {
  //         this.listQuery.page = 1
  //       }
  //     },
  //     immediate: true
  //   }
  // },
  created() {
    if (!this._.isNil(this.$route.query.page) &&
      !this._.isEmpty(this.$route.query.page)
    ) {
      this.listQuery.page = parseInt(this.$route.query.page)
    } else {
      this.listQuery.page = 1
    }
    this.fetchPlatform()
    this.fetchData()
  },
  methods: {
    fetchPlatform() {
      getPlatformList().then(response => {
        // console.log(response)
        this.platformOptions = response.data
      }).catch(err => {
        console.log(err)
      })
    },
    fetchData() {
      this.listLoading = true
      const query = {
        ...this.$route.query,
        ...this.listQuery
      }
      this.$router.push({
        query
      }).catch(err => {
        console.log(err)
      })
      getLiveBroadcastsList(this.listQuery).then(response => {
        // console.log(response)
        this.list = response.data
        this.total = response.total
        this.listLoading = false
      }).catch(err => {
        console.log(err)
      })
    },
    removeLiveBroadcast(id) {
      this.$confirm('此操作将永久删除该直播, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        removeLiveBroadcast({ id }).then(response => {
          // console.log(response)
          this.fetchData()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        }).catch(err => {
          console.log(err)
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.list = []
      this.fetchData()
    },
    resetFilter() {
      this.$refs.filterForm.resetFields()
      for (const i in this.listQuery) {
        if (i !== 'size') {
          this.listQuery[i] = undefined
        }
      }
      // console.log(this.listQuery)
      this.$router.push({
        query: {
          page: 1
        }
      }).catch(err => {
        console.log(err)
      })
      this.handleFilter()
    },
    toLiveBroadcastNew() {
      this.$router.push('/live-broadcasts/new')
    },
    toLiveBroadcastEdit(id) {
      this.$router.push(`/live-broadcasts/edit/${id}`)
    },
    // toLiveBroadcastReservation(id) {
    //   this.$router.push(`/live-broadcasts/list/${id}/reservation`)
    // },
    toLiveBroadcastRecords(id) {
      this.$router.push(`/live-broadcasts/list/${id}/records`)
    }
  }
}
</script>
